var saveOrUpdate;
var reload;
var reset;
require(['select2', 'date','layout','validator'], function () {
	$('#saas').initForm();
	var $tab = $('#tab'); 
	$('#addRow').on('click', function(){
		insertRow();
	});
	
	//新增一行
	function insertRow(){
		var $currentRow=$("#tab tr").eq(-2);
		var $row = $("<tr></tr>"); 
		var $td = $("<td style=\"border-left: 1px solid #DCDCDC\" class=\"ac\">"
                   	+"<input type=\"text\" class=\"input-text\" data-validator=\"{required:true,decimal:[3,7]}\">"
                  +"</td>"
                  +"<td class=\"ac\">"
                   +"<input type=\"text\" class=\"input-text\" data-validator=\"{required:true,decimal:[3,7]}\">"
                  +"</td>"
                  +"<td class=\"ac\">"
                  +" <a class=\"operate operate-delete\"><span class=\"del\">删除</span></a>"
                  +"</td>");
		$row.append($td); 
		$currentRow.after($row);
	}
	
	$tab.on('click','span.del',function(){
		var $rowNum = $("#tab tr").length;
		if($rowNum>3){
			$(this).closest('tr').remove();
		}
		else{
			$.messager.alert("警告","请保持最后一行数据。","info");
		}   
	});
	
	//添加或更新
	saveOrUpdate = function (fn){
		if($('#parkingId').val()){
			$.messager.confirm("是否更新", "是否更新该停车场信息？", function(r) {
				if (r) {
					if($.validator($('#saas'))){
						var $data = $('#saas').serializeArray();
						var $tr=$("#tab tr:not(:first):not(:last)");	
						var i = 0;
						$tr.each(function (key, value) {  
							var $td=$(this).children('td');
							$data.push({
								name : "gpsList["+ i +"].loLgt",
								value: $td.eq(0).find("input").val()
							});
							$data.push({
								name : "gpsList[" + i +"].loLtt",
								value: $td.eq(1).find("input").val()
							});
							i++;
						}); 
						$.ajaxExt({
							url : "/sysParking/updBaseParkingInfo.htm",
							data : $data
						}).done(function(result){
							fn();
						});
					}
				}
			});
		}
		else{
			if($.validator($('#saas'))){
				var $data = $('#saas').serializeArray();
				var $tr=$("#tab tr:not(:first):not(:last)");	
				var i = 0;
				$tr.each(function (key, value) {  
					var $td=$(this).children('td');
					$data.push({
						name : "gpsList["+ i +"].loLgt",
						value: $td.eq(0).find("input").val()
					});
					$data.push({
						name : "gpsList[" + i +"].loLtt",
						value: $td.eq(1).find("input").val()
					});
					i++;
				}); 
				$.ajaxExt({
					url : "/sysParking/addBaseParkingInfo.htm",
					data : $data
				}).done(function(result){
					fn();
				});
			}
		}
	}
	
	reload=function(data){
		//显示
		$("#liParkingLotCd").css('display','block');
		//加载基本数据
		$('#saas').setValues(data);
		//加载经纬度集
		$.ajaxExt({
			url : "/sysParking/getBaseParkingGpsListById.htm",
			data : [{name:'parkingId',value:data.parkingId}]
		}).done(function(result){
			if(result){
				$("#tab tr:not(:first):not(:last)").remove();
				$.each(result,function(index,item){
					var $rowNum = $("#tab tr").length;
					var $currentRow=$("#tab tr").eq(-2);
					var $row = $("<tr></tr>"); 
					var $td = $("<td style=\"border-left: 1px solid #DCDCDC\" class=\"ac\">"
			                   	+"<input type=\"text\" class=\"input-text\" value=\""+item.loLgt+"\" data-validator=\"{required:true}\">"
			                  +"</td>"
			                  +"<td class=\"ac\">"
			                   +"<input type=\"text\" class=\"input-text\" value=\""+item.loLtt+"\" data-validator=\"{required:true}\">"
			                  +"</td>"
			                  +"<td class=\"ac\">"
			                  +" <a class=\"operate operate-delete\"><span class=\"del\">删除</span></a>"
			                  +"</td>");
					$row.append($td); 
					$currentRow.after($row);
					});
			}
		});
        $('#provinceId').select2("readonly",true);
        $('#cityId').select2("readonly",true);
	}
	
	//重置
	reset=function(){
        $('#saas').initForm();
		//隐藏
		$("#liParkingLotCd").css('display','none');
        $('#provinceId').select2("readonly",false);
        $('#cityId').select2("readonly",false);
        $('#saas').reset();
		$("#tab tr:not(:first):not(:last)").remove();
		insertRow();
	}
});